﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.Office.Client;
using System.Diagnostics;



namespace QLCN
{
    public partial class FrmIn : Form
    {
        public static string doituong = string.Empty;
        private SqlDataAdapter sqlDataAdapter;
        private SqlConnection sqlConnection;
        private SqlCommand sqlCommand;
        private SqlDataReader sqlDataReader;
        private DataSet sqlDataSet;
        private DataTable sqlTable;
        private String Maso;
        private String Matinh = "";
        private String Mahuyen = "";
        private string ServerName = Chung.ServerName;
        private string DatabaseName = Chung.DatabaseName;
        private string UserDatabase = Chung.UserDatabase;
        private string PassWordDatabase = Chung.PasswordDatabase;
        private string ChuoiKetNoi = "";
        public FrmIn()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmIn_Load(object sender, EventArgs e)
        {
            ChuoiKetNoi = "Data Source = " + ServerName + ";" +
                      "Initial Catalog = " + DatabaseName + ";" +
                      "persist security info = true;" +
                      "User Id=" + UserDatabase +
                      "; Password='" + PassWordDatabase +
                      "'; Connect Timeout =50";

            //mo ket noi
            sqlConnection = new SqlConnection(ChuoiKetNoi);
            sqlConnection.Open();

            string dieukien = "";

            if (!string.IsNullOrEmpty(doituong))
            {
                dieukien = "IDDoiTuong =" + doituong.Trim();
            }

            string DuongDan = @"\\222.255.132.62" + Chung.DuongDanAnh + doituong.Trim() + ".JPG";

            if (dieukien.Trim() != "")
            {
                try
                {
                    sqlCommand = new SqlCommand("spINPhieuQL", sqlConnection);
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    //lấy giá trị vào biến    
                    sqlCommand.Parameters.AddWithValue("@dulieu", dieukien.Trim());

                    //thực thi truy van 
                    sqlCommand.ExecuteNonQuery();

                    SqlDataAdapter dscmd = new SqlDataAdapter(sqlCommand);
                    DataSet ds = new DataSet();

                    // Lấp đầy kết quả vào DataSet
                    dscmd.Fill(ds, "spINPhieuQL");

                    //Lấy dữ liệu từ datable ra report
                    Report.RptPhieuQl objRpt = new Report.RptPhieuQl();
                    objRpt.SetDataSource(ds.Tables[0]);

                    objRpt.SetParameterValue("pathImage", DuongDan);

                    CrView.ReportSource = objRpt;
                    CrView.Refresh();

                    sqlCommand.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(" Đang có lỗi : " + ex.Message);
                }
            }
            
        }

        private void BtnMauin_Click(object sender, EventArgs e)
        {
            try
            {
                // lay duong dan chua file doc
                string Filepath = @"\\222.255.132.62\Backup\X.P02.doc"; //Directory.GetCurrentDirectory() +@"\" + "X.P02.docx";

                //thuc thi lenh in    
                Process print = new Process();
                print.StartInfo.FileName = Filepath;

               // MessageBox.Show(Filepath);
                print.StartInfo.Verb = "Print";
                print.StartInfo.UseShellExecute = true;
                print.StartInfo.CreateNoWindow = true;
                print.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                print.Start();                

                print.CloseMainWindow();
                print.Close();

                this.Close();

            } 
            catch (Exception x)
            {
                MessageBox.Show(x.Message + "\n\n Không thể In file mẫu .", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }         

        }

        
    }
}
